package com.zhoug.zmui.dialog.list;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.zhoug.common.adapters.BaseListViewAdapter;
import com.zhoug.zmui.R;
import com.zhoug.zmui.utils.ZMUIUtils;
import com.zhoug.zmui.ints.OnItemClickListener;

import androidx.annotation.NonNull;

/**
 * 列表对话框
 * zmui_style_ZMUIListDialog
 * 定义全局样式
 * @Author 35574
 * @Date 2020/7/21
 * @Description
 */
public abstract class ZMUIListDialog<T> extends ZMUIAbsListDialog<T> {
    private static final String TAG = ">>>ZMUIListDialog";
    private static final boolean DEBUG = false;
    /**
     * item单击事件
     */
    private OnItemClickListener onItemClickListener;
    private ZMUIListAdapter mAdapter;

    public ZMUIListDialog(@NonNull Context context) {
        this(context,0);
    }

    public ZMUIListDialog(@NonNull Context context, int themeResId) {
        this(context, themeResId,R.attr.zmui_style_ZMUIListDialog,0);
    }

    protected ZMUIListDialog(@NonNull Context context, int themeResId, int defStyleAttr, int defStyleRes) {
        super(context, themeResId, defStyleAttr, defStyleRes);
    }

    @Override
    protected void initListView(ListView listView) {
        mAdapter=new ZMUIListAdapter();
        mAdapter.setData(getData());
        listView.setAdapter(mAdapter);
    }

    /**
     * 数据适配器
     */
    private class ZMUIListAdapter extends BaseListViewAdapter<T> implements View.OnClickListener {

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.zmui_list__item_sample_text, parent, false);
                TextView textView = (TextView) convertView;
                ZMUIUtils.setTextColor(textView, getTextColor());
                ZMUIUtils.setTextSize(textView, getTextSize());
                textView.setGravity(getTextGravity());
            }
            TextView textView = (TextView) convertView;
            T data = getData().get(position);
            if (data != null) {
                textView.setText(getText(data));
                if (onItemClickListener != null) {
                    convertView.setClickable(true);
                    convertView.setTag(position);
                    convertView.setOnClickListener(this);
                }
            } else {
                textView.setText(null);
                convertView.setOnClickListener(null);
            }

            return convertView;
        }

        @Override
        public void onClick(View v) {
            dismiss();
            int pos = (int) v.getTag();
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(pos);
            }
        }
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

}
